package vcp.gui.frames;

import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;

import vcp.gui.components.VCPFrame;
import vcp.gui.components.VCPMenu;
import vcp.gui.components.VCPPanel;
import vcp.gui.forms.CancelOrderForm;
import vcp.gui.secure.screens.ScreenContactCustomer;
import vcp.gui.secure.screens.ScreenRatesConfirmation;
import vcp.gui.secure.screens.ScreenResolveComplaints;
import vcp.gui.secure.screens.ScreenSaveParking;
import vcp.gui.secure.screens.ScreenSwitchCombo;
import vcp.gui.secure.screens.ScreenSystemState;
import vcp.gui.secure.screens.ScrenDynPanel;
import vcp.gui.test.entities.User;
import vcp.gui.utils.MenuBuilder;

public class SecureFrame extends VCPFrame {
	// private User currentUser;
	private User user;
	private VCPMenu menu;
	private ScreenSwitchCombo comboScreen; // switch panel container
	private ScrenDynPanel dynpanel; // switched panel

	/* network manager menu items */
	private JButton NMGRMenuItmRateConfirmal; // ishur taasrifim
	private JButton NMGRMenuItmSysState; // tmunat macav
	private JButton NMGRMenuItmReports; // dohot
	private JButton NMGRMenuItmDataRead; // kriat natunim

	/* network manager panels */
	private ScreenRatesConfirmation screenRatesConfirmation;
	private ScreenSystemState screenSystemState;

	/* Customer service menu items */
	private JButton SMGRMenuItmSaveParking; // shmirat hanayot
	private JButton SMGRMenuItmComplaintsResolval; // birur tlunot
	private JButton SMGRMenuItmContactCustomer; // shlihat odaa le lakoah

	/* Customer service panels */
	private ScreenSaveParking screenSaveParking;
	private ScreenContactCustomer  screenContactCustomer;
	private ScreenResolveComplaints  screenResolveComplaints;
	
	
	
	/* costructor sets user based on ititialization */
	public SecureFrame(User user) {
		super();
		setUser(user);
		initialize();
	}

	/* initializes frame based on role !!! */
	private void initialize() {
		String role = user.getPermission();
		switch (role) {
		case "networkManager":
			initNetworkManager();
			break;
		case "parkingEmployee":
			initParkingEmployee();
			break;
		case "parkingManager":
			initParkingManager();
			break;
		case "customerService":
			initCustomerService();
			break;
		}
	}

	/* switches content of right dynamic panel */
	private void switchPanel(VCPPanel pnl) {
		if (pnl != null) {
			dynpanel.removeAll();
			dynpanel.add(pnl);
			dynpanel.revalidate();
			dynpanel.repaint();
			pack();
		}
	}

	private void setUser(User user) {
		try {
			if (!user.getUsername().equals(null)) // change to id
				this.user = user;
		} catch (RuntimeException e) {
			System.out.println("no user .. ");
		}
	}
	
	

	private void initNetworkManager() {
		this.setTitle("VCP "+user.getPermission()+" :: " + user.getUsername());
		this.screenRatesConfirmation = new ScreenRatesConfirmation();
		this.screenSystemState = new ScreenSystemState();
		// initialize combo screen
		this.dynpanel = new ScrenDynPanel();
		this.comboScreen = new ScreenSwitchCombo(getNetworkManagerMenu(),
				dynpanel);
		this.changePanel(this.comboScreen);
	}

	private void initParkingManager() {
		this.setTitle("VCP "+user.getPermission()+" :: " + user.getUsername());

	}

	private void initParkingEmployee() {
		this.setTitle("VCP "+user.getPermission()+" :: " + user.getUsername());
	}

	private void initCustomerService() {
		this.setTitle("VCP "+user.getPermission()+" :: " + user.getUsername());
		this.screenSaveParking = new ScreenSaveParking();
		this.screenContactCustomer = new ScreenContactCustomer();
		this.screenResolveComplaints = new ScreenResolveComplaints();
		this.dynpanel = new ScrenDynPanel();
		this.comboScreen = new ScreenSwitchCombo(getCustomerServiceMenu(),
				dynpanel);
		this.changePanel(this.comboScreen);

	}

	private VCPMenu getNetworkManagerMenu() {
		MenuBuilder builder = new MenuBuilder(new Insets(5, 5, 5, 5),
				new GridLayout(4, 1, 5, 5));
		builder.addItem(getNMGRMenuItmRateConfirmal("Rate Confirmation"));
		builder.addItem(getNMGRMenuItmSysState("System State"));
		builder.addItem(getNMGRMenuItmReports("Reports"));
		builder.addItem(getNMGRMenuItmDataRead("Data Details"));

		return (VCPMenu) builder.getMenu();
	}

	private VCPMenu getCustomerServiceMenu() {
		MenuBuilder builder = new MenuBuilder(new Insets(5, 5, 5, 5),
				new GridLayout(4, 1, 5, 5));
		builder.addItem(getSMGRMenuItmSaveParking("Save parking"));
		builder.addItem(getSMGRMenuItmComplaintsResolval("Resolve Complaints"));
		builder.addItem(getSMGRMenuItmContactCustomer("Contact Customer"));

		return (VCPMenu) builder.getMenu();
	}

	/* Network manager menu items */
	private JButton getNMGRMenuItmRateConfirmal(String text) {
		if (NMGRMenuItmRateConfirmal == null) {
			NMGRMenuItmRateConfirmal = new JButton(text);
			NMGRMenuItmRateConfirmal.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					switchPanel(screenRatesConfirmation);
				}
			});
		}
		return NMGRMenuItmRateConfirmal;
	}

	private JButton getNMGRMenuItmSysState(String text) {
		if (NMGRMenuItmSysState == null) {
			NMGRMenuItmSysState = new JButton(text);
			NMGRMenuItmSysState.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					switchPanel(screenSystemState);
				}
			});
		}
		return NMGRMenuItmSysState;
	}

	private JButton getNMGRMenuItmReports(String text) {
		if (NMGRMenuItmReports == null) {
			NMGRMenuItmReports = new JButton(text);
			NMGRMenuItmReports.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					//
				}
			});
		}
		return NMGRMenuItmReports;

	}

	private JButton getNMGRMenuItmDataRead(String text) {
		if (NMGRMenuItmDataRead == null) {
			NMGRMenuItmDataRead = new JButton(text);
			NMGRMenuItmDataRead.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					//
				}
			});
		}
		return NMGRMenuItmDataRead;
	}

	/* Customer service menu items */

	private JButton getSMGRMenuItmSaveParking(String text) {
		if (SMGRMenuItmSaveParking == null) {
			SMGRMenuItmSaveParking = new JButton(text);
			SMGRMenuItmSaveParking.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					switchPanel(screenSaveParking);
				}
			});
		}
		return SMGRMenuItmSaveParking;
	}

	private JButton getSMGRMenuItmComplaintsResolval(String text) {
		if (SMGRMenuItmComplaintsResolval == null) {
			SMGRMenuItmComplaintsResolval = new JButton(text);
			SMGRMenuItmComplaintsResolval
					.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent e) {
							switchPanel(screenResolveComplaints);
							}
					});
		}
		return SMGRMenuItmComplaintsResolval;
	}

	private JButton getSMGRMenuItmContactCustomer(String text) {
		if (SMGRMenuItmContactCustomer == null) {
			SMGRMenuItmContactCustomer = new JButton(text);
			SMGRMenuItmContactCustomer.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					switchPanel(screenContactCustomer);
				}
			});
		}
		return SMGRMenuItmContactCustomer;
	}

}
